<?php
declare(strict_types=1);

namespace app\admin\service;

use app\admin\model\ArticleViews as ArticleViewsModel;

class ArticleViewsService
{
    /**
     * 文章阅读人数
     * @param array $where
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function countArticleViews($where = [])
    {
        $viewNums = 0;
        $article = ArticleService::getArticleData($where);
        foreach ($article as $value) {
            $views = ArticleViewsModel::countArticleViews(['aid' => $value['id'], 'enroll_id' => $value['enroll_id']]);
            $viewNums += $views;
        }

        return $viewNums;
    }

    /**
     * 文章阅读次数
     * @param array $where
     * @return int
     */
    public static function countArticleViewsNums($where = [])
    {
        return ArticleViewsModel::countArticleViewsNums($where);
    }

    /**
     * 统计当天文章阅读次数
     * @param array $where
     * @return mixed
     */
    public static function countTodayArticleViewsNums($where = [])
    {
        return ArticleViewsModel::countTodayArticleViewsNums($where);
    }

    /**
     * 统计当月文章阅读次数
     * @param array $where
     * @return mixed
     */
    public static function countMonthArticleViewsNums($where = [])
    {
        return ArticleViewsModel::countMonthArticleViewsNums($where);
    }

    /**
     * 统计文章阅读量
     * @param $where
     * @return float
     */
    public static function sumArticleViews($where)
    {
        return ArticleViewsModel::sumArticleViews($where);
    }

    /**
     * 统计文章当日阅读人数
     * @param array $where
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function countTodayArticlePeopleViews($where = [])
    {
        $article = ArticleService::getArticleData($where);
        $todayViews = 0;
        foreach ($article as $val) {
            $views = ArticleViewsModel::countTodayArticlePeopleViews(['aid' => $val['id'], 'enroll_id' => $val['enroll_id']]);
            $todayViews += $views;
        }
        return $todayViews;
    }

    /**
     * 统计文章昨日阅读人数
     * @param $where
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function countYesTodayArticlePeopleViews($where)
    {
        $article = ArticleService::getArticleData($where);
        $yesTodayViews = 0;
        foreach ($article as $val) {
            $views = ArticleViewsModel::countYesTodayArticlePeopleViews(['aid' => $val['id'], 'enroll_id' => $val['enroll_id']]);
            $yesTodayViews += $views;
        }
        return $yesTodayViews;
    }

    /**
     * 统计文章本月阅读人数
     * @param $where
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function countMonthArticlePeopleViews($where)
    {
        $article = ArticleService::getArticleData($where);
        $monthViews = 0;
        foreach ($article as $val) {
            $views = ArticleViewsModel::countMonthArticlePeopleViews(['aid' => $val['id'], 'enroll_id' => $val['enroll_id']]);
            $monthViews += $views;
        }
        return $monthViews;
    }

    /**
     * 访问人身份数据
     * @param array $where
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getIdentityArticleData($where)
    {
        $peopleList = ArticleViewsModel::getArticlePeopleList($where);
        return getIdentityData($peopleList);
    }

    /**
     * 文章访问人累计
     * @param $where
     * @return int
     */
    public static function countArticleViewsPeople($where)
    {
        return ArticleViewsModel::countArticleViewsPeople($where);
    }

    /**
     * 当前分类文章访问总数、用户分配
     * @param $where
     * @param int $types
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getArticleViewsPeople($where, $types = 2)
    {
        $data = OfficialStaffService::getOfficialStaff($types);
        if ($data) {
            // id作为键
            $data = array_column($data, null, 'id');
        }
        // 条件合并
        $mapArr = [];
        foreach ($data as $id => $v) {
            $where1 = [
                'enroll_id', '=', $v['id'],
            ];
            $mapArr[$id] = [$where, $where1];
        }

        $viewNums = 0;
        $usersList = [];
        foreach ($mapArr as $id => $map) {
            $article = ArticleService::getArticleData($map);
            $usersNums = 0;
            foreach ($article as $val) {
                $views = ArticleViewsModel::countArticleViews(['aid' => $val['id'], 'enroll_id' => $val['enroll_id']]);
                $usersNums += $views;
            }
            $usersList[$id]['name'] = $data[$id]['alias'];
            $usersList[$id]['usersNums'] = $usersNums;
            $viewNums += $usersNums;
        }

        return ['viewNums' => $viewNums, 'usersList' => $usersList];
    }

    /**
     * 其他分类文章访问用户
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getOtherArticleViewPeople($where)
    {
        // 获取其他分类文章数据
        $article = ArticleService::getArticleData($where);
        $data = [];
        foreach ($article as $k => $v) {
            $types = $v['types'];
            $data[$types][] = $v;
        }

        $viewNums = 0;
        $cateList = [];
        foreach ($data as $types => $arr) {
            $nums = 0;
            foreach ($arr as $kkk => $vvv) {
                $views = ArticleViewsModel::countArticleViews(['aid' => $vvv['id'], 'enroll_id' => $vvv['enroll_id']]);
                $nums += $views;
            }

            $cateList[$types]['name'] = getArticleCateName($types);
            $cateList[$types]['usersNums'] = $nums;
            $viewNums += $nums;
        }

        return ['viewNums' => $viewNums, 'cateList' => $cateList];
    }

    /**
     * 文章访问总数（折线图）
     * @param array $where
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getArticleViewsLine($where = [])
    {
        // 获取其他分类文章数据
        $article = ArticleService::getArticleData($where);
        $startEndTime = getStartEndTime();
        $marketingMonth = [];
        foreach ($article as $key => $val) {
            $data = ArticleViewsModel::getArticleViewsLine($startEndTime['startTime'], $startEndTime['endTime'], ['aid' => $val['id'], 'enroll_id' => $val['enroll_id']]);
            foreach ($data as $v) {
                $marketingMonth[] = $v;
            }
        }
        return inArr($marketingMonth, $startEndTime['dateArr']);
    }
}